﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 广州WMS货主变更
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSCustomTraceChangeController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public WMSCustomTraceChangeController(ILogger<WMSCustomTraceChangeRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<WMSCustomTraceChangeController>();

        }

        /// <summary>
        /// 广州WMS货主变更
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTraceChange([FromBody] WMSCustomTraceChangeRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSCustomTraceChangeResponseDto re = new WMSCustomTraceChangeResponseDto();
            string order_no = strparm.order_no;


            if (order_no.Trim() == "")
            {
                strMsg = "order_no入库单号不能为空，请检查后再试！";
            }

            if (strparm.customer_code_old.Trim() == ""|| strparm.customer_name_old.Trim() == ""|| strparm.customer_code_new.Trim() == ""|| strparm.customer_name_new.Trim() == "")
            {
                strMsg = "<customer_code_old,customer_name_old,customer_code_new,customer_name_new>，请检查后再试！";
            }



            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {

                    if (strparm.rows!=null)
                    {
                        string barcode_nos = "";
                        var row = strparm.rows;
                        foreach (var item in row)
                        {
                            barcode_nos = barcode_nos + item.barcode_no + ",";

                            dic = new Dictionary<string, string>();
                            dic.Add("TRACEID", item.barcode_no);
                            string sqlstr3 = MethodHelp.GetTranSQL("TMS0160", dic, con);//校验托盘信息是否存在
                            DataTable check_TRACEID = DB.OracleQuery(sqlstr3.ToString(), gz_con);//Oracle驱动查询
                            if (check_TRACEID.Rows.Count<=0)
                            {
                                strMsg = strMsg+ "托盘号=<" + item.barcode_no + ">WMS不存在相关托盘信息，请确认后再试！--";
                            }
                        }

                        //托盘信息校验成功再进行往下操作
                        if (strMsg=="")
                        {
                            string barcode = barcode_nos.TrimEnd(',');
                            barcode = MethodHelp.ChangeCommas(barcode);
                            dic = new Dictionary<string, string>();
                            dic.Add("TRACEID", barcode);
                            dic.Add("货主编码", strparm.customer_code_old);
                            string sqlstr = MethodHelp.GetTranSQL("TMS0158", dic, con);//获取变更托盘原货主
                            DataTable check_ccode = DB.OracleQuery(sqlstr.ToString(), gz_con);//Oracle驱动查询
                            if (check_ccode.Rows.Count > 0)
                            {
                                strMsg = strMsg + "--条码<" + check_ccode.Rows[0]["TRACEID"].ToString() + ">原货主=<" + check_ccode.Rows[0]["HEDI17"].ToString() + "> ，与申请数据<" + strparm.customer_name_old + ">不一致，托盘原货主错误，更新失败！";
                            }
                            else
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("货主编码", strparm.customer_code_new);
                                dic.Add("CRM单据号", strparm.order_no);
                                dic.Add("货主", strparm.customer_name_new);
                                dic.Add("库存来源", "货主变更");
                                dic.Add("是否有主", "是");
                                dic.Add("销售部门", strparm.department_name);
                                dic.Add("TRACEID", barcode);
                                string sqlstr2 = MethodHelp.GetTranSQL("TMS0157", dic, con);//获取变更托盘原货主
                                if (sqlstr2 != "")
                                {
                                    strSqlList.Add(sqlstr2.ToString());
                                }

                                if (strSqlList.Count > 0)
                                {
                                    bool A = DB.OracleExecuteNoQueryTran(strSqlList, gz_con);
                                    if (A)
                                    {

                                    }
                                    else
                                    {
                                        strMsg = "TMS0157 Database update failed";
                                    }

                                }
                                else
                                {
                                    strMsg = "No insertion data present";
                                }
                            }
                        }
                       
                    }

                   
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
